<?php

include_once ('db_manage.php');

function getArray($sql) {
    $array = array();
    $result = mysql_query($sql);
    $i = 0;
    while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) != false) {
        $array [$i] = $row;
        $i++;
    }
    mysql_free_result($result);
    return $array;
}

function update($sql) {
    if (mysql_query($sql)) {
        return mysql_insert_id();
    }
}

function updateHot($id) {
    $sql = "update pcd_you set hot = hot + 1 where id = " . $id;
    mysql_query($sql);
}

function getTopYouApprove($count) {
    $sql = "select * from pcd_you where status = 1 order by id desc limit 0," . $count;
    return getArray($sql);
}

function getTotalYouCountApprove() {
    $sql = "select count(*) as total from pcd_you where status = 1";
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $total = $rs->total;
    }
    mysql_free_result($result);
    return $total;
}

function getYouByPageApprove($page, $num_row_of_page) {
    $start = $page * $num_row_of_page;
    $sql = "select y.*,(SELECT username FROM cdb_members WHERE y.postuser = uid) AS postname from pcd_you y where status = 1 order by createTime desc limit " . $start . ", " . $num_row_of_page;
    return getArray($sql);
}

function getTotalYouCountApproveByPlayID($playID) {
    $sql = "select count(*) as total from pcd_you where status = 1 AND fid = " . $playID;
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $total = $rs->total;
    }
    mysql_free_result($result);
    return $total;
}

function getYouByPageApproveAndPlayID($page, $num_row_of_page, $playID) {
    $start = $page * $num_row_of_page;
    $sql = "select y.*,(SELECT username FROM cdb_members WHERE y.postuser = uid) AS postname from pcd_you y where status = 1 AND fid = " . $playID . " order by createTime desc limit " . $start . ", " . $num_row_of_page;
    return getArray($sql);
}

function getTopYou($count) {
    $sql = "select * from pcd_you WHERE status = 1 order by id desc limit 0," . $count;
    return getArray($sql);
}

function getTotalYouCount() {
    $sql = "select count(*) as total from pcd_you";
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $total = $rs->total;
    }
    mysql_free_result($result);
    return $total;
}

function getYouByPage($page, $num_row_of_page) {
    $start = $page * $num_row_of_page;
    $sql = "select y.*,(SELECT username FROM cdb_members WHERE y.postuser = uid) AS postname from pcd_you y order by createTime desc limit " . $start . ", " . $num_row_of_page;
    return getArray($sql);
}

function getTotalYouCountByPlayID($playID) {
    $sql = "select count(*) as total from pcd_you WHERE fid = " . $playID;
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $total = $rs->total;
    }
    mysql_free_result($result);
    return $total;
}

function getYouByPageAndPlayID($page, $num_row_of_page, $playID) {
    $start = $page * $num_row_of_page;
    $sql = "select y.*,(SELECT username FROM cdb_members WHERE y.postuser = uid) AS postname from pcd_you y WHERE fid = " . $playID . " order by createTime desc limit " . $start . ", " . $num_row_of_page;
    return getArray($sql);
}

function getYouById($id) {
    $sql = "select y.*,(SELECT username FROM cdb_members WHERE y.postuser = uid) AS postname from pcd_you y where id =" . $id;
    $array = getArray($sql);
    if (count($array) > 0) {
        return $array [0];
    }
}

function createYou($you) {
    $sql = "insert into pcd_you(title,fid,module,content,facepic,smallpic,postuser,postname,postdate,status,createTime) values('" . $you ['title'] . "'," . $you['fid'] . ",'" . $you['module'] . "','" . $you ['content'] . "','" . $you ['facepic'] . "','" . $you ['smallpic'] . "'," . $you ['postuser'] . ",'" . $you ['postname'] . "',now(),0,now())";
    return update($sql);
}

function updateYou($you) {
    $sql = "UPDATE pcd_you SET title = '" . $you ['title'] . "',content = '" . $you ['content'] . "',facepic = '" . $you ['facepic'] . "',smallpic='" . $you ['smallpic'] . "' WHERE id = " . $you ['id'];
    if ($you ['facepic'] == "") {
        $sql = "UPDATE pcd_you SET title = '" . $you ['title'] . "',content = '" . $you ['content'] . "' WHERE id = " . $you ['id'];
    }
    //	echo $sql;
    update($sql);
}

function approveYou($youID) {
    $sql = "update pcd_you set status = 1";
    update($sql);
}

?>